const modules: any = import.meta.glob('../router/main/**/**.ts', {
  eager: true
})
// const routeFiles = require.context("../router/main", true, /\.ts/); webpack下的

export function mapMenusToRoutes(menus: any[]) {
  const routeFiles: any[] = []
  const allRoutes: any[] = []

  for (const path in modules) {
    routeFiles.push(modules[path].default)
  }

  const _recurseGetRoute = function (menus: any[]) {
    for (const menu of menus) {
      if (menu.type === 2) {
        const route = routeFiles.find((item) => item.path === menu.url)
        if (route) {
          allRoutes.push(route)
        }
      } else {
        _recurseGetRoute(menu.children)
      }
    }
  }
  _recurseGetRoute(menus)
  console.log(allRoutes)
  return allRoutes
}
/**
 * 根据路径来匹配属于哪个路由对象menu
 */
export function pathToMenu(menus: any[], path: string) {
  let currentMenu = null
  const recursePathToMenu = function (menus: any[]) {
    for (const menu of menus) {
      // console.log(menu.url);
      if (menu.type === 2) {
        if (menu.url === path) {
          currentMenu = menu
          return //这个retun只是停止了当前寻找到的子路由的循环，并没有停止下一个路由的循环
        }
      } else {
        recursePathToMenu(menu.children)
      }
    }
  }
  recursePathToMenu(menus)
  return currentMenu
}

/**
 * 根据菜单列表来映射id
 */
export function mapMenusToId(menu: any[]) {
  const idArr: number[] = []
  console.log(menu)

  const recurseMapMenusToId = function (menu: any[]) {
    for (const item of menu) {
      if (item.children) {
        recurseMapMenusToId(item.children)
      } else {
        idArr.push(item.id)
      }
    }
  }
  recurseMapMenusToId(menu)
  return idArr
}

/**
 * 根据菜单route来查询属于哪个菜单,返回层级数组
 */
export function routeToMenuArr(route: string, menu: any[]) {
  const menuArr: any[] = []
  const recurseRouteToMenuArr = function (menu: any[]) {
    for (const item of menu) {
      if (item.children) {
        //如果是有二级存在
        for (const citem of item.children) {
          if (route === citem.url) {
            menuArr.push(item)
            menuArr.push(citem)
          }
        }
      } else {
        if (route === item.url) {
          menuArr.push(item)
        }
      }
    }
  }
  recurseRouteToMenuArr(menu)
  return menuArr
}

/**
 *根据菜单route来获取对应的menu然后转成自己对应需要的数据格式
 *
 */

export function routeToMenuToTabObj(route: string, menu: any[]) {
  let tabObj: any = {}
  const recurseRouteToMenuToTabObj = function (menu: any[]) {
    for (const item of menu) {
      if (item.children) {
        //如果是有二级存在
        for (const citem of item.children) {
          if (route === citem.url) {
            tabObj = {
              title: citem.name,
              id: citem.id + '',
              url: citem.url
            }
          }
        }
      } else {
        if (route === item.url) {
          tabObj = {
            title: item.name,
            id: item.id + '',
            url: item.url
          }
        }
      }
    }
  }
  recurseRouteToMenuToTabObj(menu)
  return tabObj
}

/**
 * 从菜单映射到按钮的权限
 * @param menuList 菜单的列表
 * @returns 权限的数组(字符串数组)
 */

export function menuToPermission(menu: any[]) {
  const permissionsArr: any[] = []
  const recurseMenuToPermission = function (menu: any[]) {
    for (const item of menu) {
      if (item.type === 3) {
        permissionsArr.push(item.permission)
      } else {
        if (item.children) recurseMenuToPermission(item.children)
      }
    }
  }
  recurseMenuToPermission(menu)
  return permissionsArr
}
